package io.nutz.nutzsite.module.exam.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import io.nutz.nutzsite.module.exam.models.Qtype;
import io.nutz.nutzsite.module.exam.services.QtypeService;
import io.nutz.nutzsite.common.base.Result;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Lang;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.plugins.slog.annotation.Slog;
import io.nutz.nutzsite.common.utils.ShiroUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Date;
/**
 * 题型 信息操作处理
 * 
 * @author lrw
 * @date 2020-04-08
 */
@IocBean
@At("/exam/qtype")
public class QtypeController {
	private static final Log log = Logs.get();

	@Inject
	private QtypeService qtypeService;
	
	@RequiresPermissions("exam:qtype:view")
	@At("")
	@Ok("th:/exam/qtype/qtype.html")
	public void index(HttpServletRequest req) {

	}

	/**
	 * 查询题型列表
	 */
	@RequiresPermissions("exam:qtype:list")
	@At
	@Ok("json")
	public Object list(@Param("pageNum")int pageNum,
					   @Param("pageSize")int pageSize,
					   @Param("name") String name,
					   @Param("id") Integer qtid,
					   @Param("orderByColumn") String orderByColumn,
					   @Param("isAsc") String isAsc,
					   HttpServletRequest req) {
		Cnd cnd = Cnd.NEW();
		if (!Strings.isBlank(name)){
			cnd.and("name", "like", "%" + name +"%");
		}
		if(qtid!=null){
			cnd.and("id", "=", qtid);
		}
		return qtypeService.tableList(pageNum,pageSize,cnd,orderByColumn,isAsc,null);
	}

	/**
	 * 新增题型
	 */
	@At("/add")
	@Ok("th:/exam/qtype/add.html")
	public void add( HttpServletRequest req) {

	}

	/**
	 * 新增保存题型
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("exam:qtype:add")
	@Slog(tag="题型", after="新增保存题型 id=${args[0].id}")
	public Object addDo(@Param("..") Qtype qtype,HttpServletRequest req) {
		try {
			qtypeService.insert(qtype);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 修改题型
	 */
	@At("/edit/?")
	@Ok("th:/exam/qtype/edit.html")
	public void edit(int id, HttpServletRequest req) {
		Qtype qtype = qtypeService.fetch(id);
		req.setAttribute("qtype",qtype);
	}

    /**
     * 详情题型
     */
    @At("/detail/?")
    @Ok("th:/exam/qtype/detail.html")
    public void detail(int id, HttpServletRequest req) {
		Qtype qtype = qtypeService.fetch(id);
        req.setAttribute("qtype",qtype);
    }

    /**
	 * 修改保存题型
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("exam:qtype:edit")
	@Slog(tag="题型", after="修改保存题型")
	public Object editDo(@Param("..") Qtype qtype,HttpServletRequest req) {
		try {
			if(Lang.isNotEmpty(qtype)){
				qtypeService.update(qtype);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 删除题型
	 */
	@At("/remove")
	@Ok("json")
	@RequiresPermissions("exam:qtype:remove")
	@Slog(tag ="题型", after= "删除题型:${array2str(args[0])}")
	public Object remove(@Param("ids")String[] ids, HttpServletRequest req) {
		try {
			qtypeService.delete(ids);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

}
